﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BskLeague.Models;
using System.Web.Security;

namespace BskLeague.Controllers
{
    public class NewsController : Controller
    {
        BskLeagueDB _db = new BskLeagueDB();

        public ActionResult Index()
        {
            var newses = (from m in _db.Newses
                          where m.newsActive == true
                          select m).ToList();

            return View(newses);
        }

       public ActionResult Details(int id)
        {
            var news = _db.Newses.Find(id);

            return View(news);
        }

       [Authorize(Roles = "Admin")]
       public ActionResult Edit(int id)
       {
           var news = _db.Newses.Find(id);
           var categories = _db.NewsCategories.ToList();
           List<SelectListItem> categoriesForDropDown = categories.Select(c => new SelectListItem
           {
                    Value = c.categoryId.ToString(),
                    Text = c.categoryName.ToString()
           }).ToList();

           ViewBag.categories = categoriesForDropDown.ToList();

           return View(news);
       }

       [Authorize(Roles = "Admin")]
       public ActionResult Delete(int id)
       {
           var news = _db.Newses.Find(id);
           news.newsActive = false;

           _db.Entry(news).State = System.Data.EntityState.Modified;
           _db.SaveChanges();

           return RedirectToAction("Index");
       }

       [Authorize(Roles = "Admin")]
       public ActionResult DeleteComment(int id)
       {
           var comment = _db.NewsComments.Find(id);
           var newsId = comment.commentNews.newsId;

           _db.NewsComments.Remove(comment);
           _db.SaveChanges();

           return RedirectToAction("Details", new { id = newsId });
       }

       [Authorize(Roles = "Admin")]
       public ActionResult New()
       {
           var categories = _db.NewsCategories.ToList();
           List<SelectListItem> categoriesForDropDown = categories.Select(c => new SelectListItem
           {
               Value = c.categoryId.ToString(),
               Text = c.categoryName.ToString()
           }).ToList();

           ViewBag.categories = categoriesForDropDown.ToList();

           return View();
       }

       [Authorize(Roles = "Admin")]
       [HttpPost]
       public ActionResult Create(string newsTitle, string newsContent, int newsCategory)
       {
           News news = new News();
           news.newsTitle = newsTitle;
           news.newsContent = newsContent;

           var category = (from m in _db.NewsCategories
                         where m.categoryId == newsCategory
                         select m).First();

           news.newsCategory = category;
           news.newsDate = DateTime.Now;
           news.newsActive = true;           
                      

            _db.Newses.Add(news);
            _db.SaveChanges();
            return RedirectToAction("Index");
       }

       [Authorize(Roles = "Admin")]
       [HttpPost]
       public ActionResult Update(int newsId, string newsTitle, string newsContent, int newsCategory)
       {
           var news = _db.Newses.Find(newsId);
           news.newsTitle = newsTitle;
           news.newsContent = newsContent;
           news.newsActive = true;
           var category = (from m in _db.NewsCategories
                           where m.categoryId == newsCategory
                           select m).First();

           news.newsCategory = category;

           _db.Entry(news).State = System.Data.EntityState.Modified;
           _db.SaveChanges();
           return RedirectToAction("Index");
       }

       [Authorize]
       [HttpPost]
       public ActionResult LeaveComment(int newsId, string commentContent)
       {
           var comment = new NewsComment();
           comment.commentNews = _db.Newses.Find(newsId);
           comment.commentContent = commentContent;
           
           MembershipUser currentUser = Membership.GetUser(User.Identity.Name, true);
           Player player = (from m in _db.Players
                            where m.UserName == currentUser.UserName
                            select m).First();
           if (player != null)
           {
                comment.commentAuthor = player;
           }

           comment.commentDate = DateTime.Now;
           
           _db.NewsComments.Add(comment);
           _db.SaveChanges();
           return RedirectToAction("Details", new { id = newsId });
       }

        
    }
}
